package cn.tedu.tea.service;

import cn.tedu.tea.domain.Attach;
import cn.tedu.tea.domain.param.AttachAddNewDTO;
import cn.tedu.tea.domain.param.AttachUpdateDTO;
import cn.tedu.tea.domain.vo.AttachListVO;
import cn.tedu.tea.domain.vo.AttachStandardVO;
import cn.tedu.tea.ex.ServiceException;
import cn.tedu.tea.mapper.AttachMapper;
import cn.tedu.tea.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class AttachServiceImpl implements IAttachService {

    @Autowired
    private AttachMapper attachMapper;

    @Override
    public void addNew(AttachAddNewDTO attachAddNewDTO) {
        log.debug("开始处理【添加附件】的业务，参数： {}", attachAddNewDTO);
        int count = attachMapper.countByFileUuid(attachAddNewDTO.getFileUuid());
        if (count > 0){
            String message = "添加附件失败，系统文件名已被占用！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        Attach attach = new Attach();
        BeanUtils.copyProperties(attachAddNewDTO, attach);
        attach.setModifiedUserId(0L).setModifiedUserName("");
        int rows = attachMapper.insert(attach);
        if (rows != 1){
            String message = "添加附件失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_INSERT, message);
        }
    }

    @Override
    public void deleteById(Integer id) {
        log.debug("开始处理【根据id删除附件】的业务，参数： {}", id);
        AttachStandardVO attachStandard = attachMapper.getStandardById(id);
        if (attachStandard == null){
            String message = "删除附件失败，要删除的附件不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        int rows = attachMapper.deleteById(id);
        if (rows != 1){
            String message = "删除附件失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_DELETE, message);
        }
    }

    @Override
    public void deleteByIds(Integer[] ids) {
        log.debug("开始处理【根据id批量删除附件】的业务，参数： {}", ids);
        int rows = attachMapper.deleteByIds(ids);
        if (rows < 1){
            String message = "删除附件失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_DELETE, message);
        }
    }

    @Override
    public void updateInfoById(AttachUpdateDTO attachUpdateDTO) {
        log.debug("开始处理【修改附件信息】的业务，参数：{}", attachUpdateDTO);
        AttachStandardVO attachStandard = attachMapper.getStandardById(attachUpdateDTO.getId());
        if (attachStandard == null){
            String message = "修改附件失败，要访问的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        int count = attachMapper.countByFileUuidAndNotId(attachUpdateDTO.getFileUuid(), attachUpdateDTO.getId());
        if (count > 0){
            String message = "修改附件失败，系统文件名已被占用！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        Attach attach = new Attach();
        BeanUtils.copyProperties(attachUpdateDTO, attach);
        int rows = attachMapper.update(attach);
        if (rows != 1){
            String message = "修改附件失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }
    }

    @Override
    public void setDisplay(Integer id) {
        log.debug("开始处理【将附件设置为上架状态】的业务，参数：{}", id);
        updateStatusById(id, 0);
    }

    @Override
    public void setHidden(Integer id) {
        log.debug("开始处理【将附件设置为下架状态】的业务，参数：{}", id);
        updateStatusById(id, 1);
    }

    @Override
    public AttachStandardVO getStandardById(Integer id) {
        log.debug("开始处理【根据id查询附件详情】的业务，参数： {}", id);
        AttachStandardVO attachStandard = attachMapper.getStandardById(id);
        if (attachStandard == null){
            String message = "查询附件详情失败，要查询的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        return attachStandard;
    }

    @Override
    public List<AttachListVO> list() {
        log.debug("开始处理【查询附件列表】的业务，无参数");
        List<AttachListVO> list = attachMapper.list();
        return list;
    }

    private void updateStatusById(Integer id, Integer status) {
        AttachStandardVO currentAttach = attachMapper.getStandardById(id);
        if (currentAttach == null) {
            String message = STATUS_TEXT[status] + "附件失败，附件数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        if (currentAttach.getStatus() == status) {
            String message = STATUS_TEXT[status] + "附件失败，此附件已经处于" + STATUS_TEXT[status] + "状态！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        Attach updateAttach = new Attach();
        updateAttach.setId(id);
        updateAttach.setStatus(status);
        int rows = attachMapper.update(updateAttach);
        if (rows != 1) {
            String message = STATUS_TEXT[status] + "附件失败，服务器忙，请稍后再尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }
    }
}
